﻿// ----------------------------------------------------------------------
// <author name="Moses Kehengu"/>
// <copyright file="Program.cs" company="Futures Group International">
//     Copyright statement. All right reserved
// </copyright>
//
// ------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using GenericTools;
using System.Configuration;
using System.Text;
using DataModels.Enumerations;
using DataModels.Common;

namespace ReportingEngineGUI
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] arguments)
        {
            bool autoStart = false;
            if (arguments.Length == 1)
            {
                if (arguments[0].ToLower() == "start") autoStart = true;
                else autoStart = false;
            }
            else autoStart = false;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);            
            
            string connection = "ReportingEngineGUI.Properties.Settings.DBConnectionString";
#if DEBUG
            AppConfiguration.DBConnectionString = @"Data Source=41.73.195.220\sql2008r2;Initial Catalog=IQSMS_TestDB;Persist Security Info=True;User ID=sa;Password=c0nstella";
            AppConfiguration.DBProviderName = ConfigurationManager.ConnectionStrings[connection].ProviderName;
#else
            AppConfiguration.DBConnectionString = ConfigurationManager.ConnectionStrings[connection].ConnectionString;
            AppConfiguration.DBProviderName = ConfigurationManager.ConnectionStrings[connection].ProviderName;
#endif

            AppConfiguration.ApplicationName = Properties.Settings.Default.ApplicationName;
            AppConfiguration.AccessMethod = DataAccessMethod.DIRECT_ACCESS;
            AppConfiguration.DataCached = false;
            
            AppConfiguration.EnableErrorLogMail = true;
            AppConfiguration.ErrorLogEmailAddress = Properties.Settings.Default.ErrorLogEmailAddress;
            AppConfiguration.LogFileDirectory = Properties.Settings.Default.logFileDirectory;
            
            AppConfiguration.MaximumReportDelay = Properties.Settings.Default.MaximumReportDelay;
            AppConfiguration.MaximumErrorTolerance = Properties.Settings.Default.MaximumErrorTolerance;
            
            AppConfiguration.ReportingEngineSMSCheckDelay = Properties.Settings.Default.ReportingEngineSMSCheckDelay;
            AppConfiguration.SameMonthReporting = Properties.Settings.Default.SameMonthReporting;

            AppConfiguration.EnableStockInformation = Properties.Settings.Default.EnableStockInformation;
            AppConfiguration.EnableAutomaticDataMerging = Properties.Settings.Default.EnableAutomaticDataMerging;
            AppConfiguration.OverwriteInAutoMerge = Properties.Settings.Default.OverwriteInAutoMerge;
            AppConfiguration.StockReportID = Properties.Settings.Default.StockReportID;

            bool errorOccured = false;
            ReportingEngineGUI reportingEngineGUI = null;
            while (true)
            {
                try
                {
                    reportingEngineGUI = new ReportingEngineGUI(autoStart);
                    errorOccured = false;
                    Application.Run(reportingEngineGUI);
                }
                catch (Exception ex)
                {
                    errorOccured = true;
                    Utilities.LogError(ex, null);

                    if (reportingEngineGUI != null)
                    {
                        reportingEngineGUI.ForceRestart();
                        reportingEngineGUI.Close();
                        reportingEngineGUI.Dispose();
                    }
                    
                    GC.Collect();
                    System.Threading.Thread.Sleep(3000);
                }
                if (errorOccured) continue;
                else break;
            }
            
        }
    }
}
